package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/2/28 14:46
 * @description
 */
public class Code849 {
    public static void main(String[] args) {
        System.out.println(new Code849().maxDistToClosest(new int[] {0, 0, 0, 1}));
    }

    public int maxDistToClosest(int[] seats) {
        int result = 1, count = 0;
        boolean flag = false;
        int len = seats.length - 1;
        for (int i = 0; i <= len; i++) {
            //第一位和最后一位为0要特殊处理
            if ((i == 0 && seats[0] == 0) || (i == len && seats[len] == 0)) {
                flag = true;
            }
            if (seats[i] == 0) {
                count++;
            } else {
                if (flag) {
                    result = Math.max(result, count);
                } else {
                    result = Math.max(result, (count + 1) / 2);
                }
                count = 0;
                flag = false;
            }
        }
        if (flag) {
            result = Math.max(result, count);
        }
        return result;
    }
}
